package com.example.demo.interceptor;

import com.example.demo.constant.Constants;
import com.example.demo.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader(Constants.USER_LOGIN_TOKEN);
        Claims claims = JwtUtils.parseToken(header);
        if (claims==null){
            log.info("获取到的token为 token {}",handler);
            response.setStatus(401);
        }
        return true;
    }
}
